/**
 * Copyright (c)  FESCOTech Team
 * All rights reserved.
 *
 * This file WriteAdvice.java creation date: [2018年11月15日 上午9:58:27] by 姓名
 * http://www.fescoetc.com.cn
 */

package com.hdsxtech.smsoot_timer.common.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import com.hdsxtech.smsoot_timer.common.annotation.Write;
import com.hdsxtech.smsoot_timer.common.datasource.DatabaseContextHolder;
import com.hdsxtech.smsoot_timer.common.datasource.DatabaseType;
 

/** 
 * <descption>执行写注解</descption> 
 * @author maewn
 * @version 1.0.0
 */
@Aspect
@Component
@EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true)
public class WriteAdvice implements Ordered {

	public static final Logger LOGGER = LoggerFactory.getLogger(WriteAdvice.class);
	
	@Around("@annotation(write)") // 在注解上加入切人点语法，实现方法
	public Object proceed(ProceedingJoinPoint proceedingJoinPoint,Write write) throws Throwable{
		
		try {
			LOGGER.info("-----------------set database connection write only---------------------");
			DatabaseContextHolder.setDatabaseType(DatabaseType.MASTER);
			return proceedingJoinPoint.proceed();  // 让这个方法执行完毕
		}finally{
			DatabaseContextHolder.clear();
			LOGGER.info("---------------clear database connection---------------");
		}
	}
	
	
 	@Override
	public int getOrder() {
		return 1;
	}

}
